package com.hupu.filters;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

//zuulfitter是Zuul中的过滤器顶级父类
@Component
public class FilterDemo extends ZuulFilter {

    @Override
    public String filterType() {
        //校验,前置拦截
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 1;
    }

    @Override
    public boolean shouldFilter() {
        //设置为true,说明说明过滤器生效了,但是否过滤拦截需要下面的run()方法具体实现
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        //获取当前的资源管理器(context,上下文)
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String user = request.getParameter("pppp");
        //判断
        if (StringUtils.isEmpty(user)){
            //放行不理会.拦截单独处理
            //不响应
            currentContext.setSendZuulResponse(false);
            currentContext.setResponseStatusCode(401);
        }

        return null;
    }
}
